# -*- coding: UTF-8 -*-
#### 使用字典

bob = {'name': 'Bob Smith', 'age': 42, 'pay': 30000, 'job': 'dev'}
sue = {'name': 'Sue Jones', 'age': 45, 'pay': 40000, 'job': 'hdw'}
print(bob['name']) # 结果：Bob Smith
print(sue['pay']) # 结果： 40000
print(bob['name'].split()[-1])  # 结果：Smith
sue['pay'] *= 1.10
print(sue['pay'])  # 结果：44000.0

#### 一次一个字段的填写字典
sue2 = {}
sue2['name'] = 'Sue Jones'
sue2['age'] = 45
sue2["pay"] = 40000
sue2['job'] = 'hdw'
print(sue2)
# 结果：{'first': 'Bob', 'last': 'Smith'}

#### 使用 zip 函数，将名/值列表链接在一起
names = ['name', 'age', 'pay', 'job']
values = ['Sue Jones', 45, 40000, 'hdw']
lis = list(zip(names, values))
print(lis) # 结果：[('name', 'Sue Jones'), ('age', 45), ('pay', 40000), ('job', 'hdw')]

sue3 = dict(zip(names, values))
print(sue3)  # 结果：{'name': 'Sue Jones', 'age': 45, 'pay': 40000, 'job': 'hdw'}

#### 初始化空字典
fields = ('name', 'age', 'job', 'pay')
record = dict.fromkeys(fields, '?')
print(record)  # 结果： {'name': '?', 'age': '?', 'job': '?', 'pay': '?'}

#### 字典列表
people = [bob, sue]  # 引用列表
for person in people:
    print(person['name'], person['pay'], sep=', ')  # 所有者的名字和薪水
# 结果：
# Bob Smith, 30000
# Sue Jones, 44000.0

for person in people:
    if person['name'] == 'Sue Jones':  # 获取 sue 的薪水
        print(person['pay'])
# 结果：44000.0

names = [person['name'] for person in people]  # 收集姓名
print(names)
# 结果：['Bob Smith', 'Sue Jones']

names = list(map((lambda x: x['name']), people))  # 同上
print(names)
# 结果：['Bob Smith', 'Sue Jones']

total_pay = sum(person['pay'] for person in people)  # 汇总薪水
print(total_pay)
# 结果：74000.0

#### 像列表解析和按需生成表达式这样的工具，可以达到类似 SQL 查询的效果， 只是这里是操作内存中的对象
names = [rec['name'] for rec in people if rec['age'] >= 45]
print(names)  # 结果：['Sue Jones']

print([(rec['age'] ** 2 if rec['age'] >= 45 else rec['age']) for rec in people]) # 结果：[42, 2025]

G = (rec['name'] for rec in people if rec['age'] >= 45)
print(next(G))  # 结果：Sue Jones

G = ((rec['age'] ** 2 if rec['age'] >= 45 else rec['age']) for rec in people)
print(G.__next__())

#### 嵌套结构
bob2 = {'name': {'first': 'Bob', 'last': 'Smith'},
            'age': 42,
            'job': ['software', 'writing'],
            'pay': (40000, 50000)
        }
print(bob2['name'])  # bob2 的全名
# 结果： {'first': 'Bob', 'last': 'Smith'}

print(bob2['name']['last'])  # bob2 的姓氏
# 结果： Smith

print(bob2['pay'][1])  # bob2 的最高薪酬
# 结果： 50000

for job in bob2['job']: print(job)  # bob2 的所有工作
# 结果：
# software
# writing

print(bob2['job'][-1])  # bob2 最近的工作
# 结果： writing

bob2['job'].append('janitor') # bob2 得到一份新工作
print(bob2['job'])
# 结果： ['software', 'writing', 'janitor']

#### 字典的字典
bob = dict(name='Bob Smith', age = 42, pay = 300000, job = 'dev')
sue = dict(name='Sue Jones', age = 45, pay = 400000, job = 'hdw')
db = {}
db['bob'] = bob  # 引用字典的字典
db['sue'] = sue
print(db['bob']['name']) # 获取 bob 的名字
# 结果：Bob Smith

db['sue']['pay'] = 50000 # 改变 sue 的薪水
print(db['sue']['pay']) # 获取 sue 的薪水
# 结果： 50000

print(db)
# 结果：
# {'bob': {'name': 'Bob Smith', 'age': 42, 'pay': 300000, 'job': 'dev'}, 'sue': {'name': 'Sue Jones', 'age': 45, 'pay': 50000, 'job': 'hdw'}}

import pprint
pprint.pprint(db)
# 结果：
# {'bob': {'age': 42, 'job': 'dev', 'name': 'Bob Smith', 'pay': 300000},
#  'sue': {'age': 45, 'job': 'hdw', 'name': 'Sue Jones', 'pay': 50000}}

#### 字典迭代器（for循环一次产生一个键）
for key in db:
    print(key, '=>', db[key]['name'])
# 结果：
# bob => Bob Smith
# sue => Sue Jones

for record in db.values():
    print(record['pay'])
# 结果：
# 300000
# 50000

x = [db[key]['name'] for key in db]
print(x)
# 结果：
# ['Bob Smith', 'Sue Jones']

x = [key['name'] for key in db.values()]
print(x)
# 结果：
# ['Bob Smith', 'Sue Jones']

#### 字典新增记录
db['tom'] = dict(name='Tom',age = 50, job = None, pay = 0)
pprint.pprint(db)
# 结果：
# {'bob': {'age': 42, 'job': 'dev', 'name': 'Bob Smith', 'pay': 300000},
#  'sue': {'age': 45, 'job': 'hdw', 'name': 'Sue Jones', 'pay': 50000},
#  'tom': {'age': 50, 'job': None, 'name': 'Tom', 'pay': 0}}

print(list(db.keys()))
# 结果：['bob', 'sue', 'tom']

print([rec['name'] for rec in db.values() if rec['age'] >= 48])
# 结果：['Tom']